<?php
/**
 * PHP Daycare
 * This file will allow the viewing of the information for currently checked in children
 * @author Jason Butz
 * @version 0.0
 * @package PHPDaycare
 */
include_once("functions.php");
include_once("config.php");
// The user MUST be logged in to view this page!
session_start();
if(!$session->isloggedin())
{
	echo "<p>You must be logged in to view this page. Please <a href=\"index.php\">login</a> to continue.</p>";
}
else // They are logged in!
{
	if(!isset($_GET['cid']))
	{
		// We need to get a list of children from the database
		$r = $db->query("SELECT ID, name FROM `child` WHERE (ID) IN (SELECT attendance.child_id As `ID` FROM attendance WHERE attendance.out IS NULL);");
		echo "
		<p>Please select the child you wish to view. Only children who are checkin may be viewed.</p>
		<table cellspacing='1' class='tablesorter' width='100%'>
		<thead>
			<tr>
				<th class=\"{sorter: 'text'}\">Name</th>
				<th class=\"{sorter: 'text'}\">Options</th>
			</tr>
		</thead>
		<tbody>";
		while($row = mysql_fetch_row($r))
		{
			echo "<tr>
						<td>" . $row[1] . "</td>
						<td><a href= \"#\" onClick=\"loadinfo('view.php', 'cid=".$row[0]."')\">View</a></td>
					</tr>";
		}
		echo "</tbody></table>";
		$temp->tablePager();
		echo "<script type=\"text/javascript\">loadsorter()</script>";
	}
	else
	{
		$cid = $_GET['cid'];
		// A child has been selected. We must verify the information
		// Does this child exist?
		$r = $db->query("SELECT ID, name FROM child WHERE ID=\"$cid\";");
		if(!mysql_fetch_row($r))
		{
			// No such child exists!
			echo "<p>Error: No such child exists!</p>";
		}
		else
		{
			$child = $db->query("SELECT * FROM child WHERE ID=$cid;");
			$parent = $db->query("SELECT ID, name FROM `c_a_rel` JOIN `adults` ON c_a_rel.adults = adults.ID WHERE child = $cid");
			//Process DB stuff
			while($row = mysql_fetch_row($parent))
			{
				$adults .= "<p><a class='button' href='#' onClick=\"placePhoto(".$row[0].", 'a');placeTitle('".$row[1]."');\">".$row[1]."</a></p>";
			}
			$child = $row = mysql_fetch_row($child);
			//
			//Output HTML
			echo "
				<table width='100%' cellspacing='1' cellpadding='1'>
				  <tr>
					 <td colspan='3'><div align='center'><strong>".$child[1]."</strong></div></td>
				  </tr>
				  <tr colspan='3'>
					 <td colspan='3'><div align='center'><input type='button' class='button' value='View Photo' onClick=\"placePhoto(".$child[0].", 'c');placeTitle('".$child[1]."');\"></div></td>
				  </tr>
				  <tr>
					 <td width='20%'>Allergies:</td>
					 <td colspan='2'>".nl2br($child[4])."</td>
				  </tr>
				  <tr colspan='3'>
					 <td>&nbsp;</td>
				  </tr>
				  <tr>
					 <td width='20%'>Medical Conditions:</td>
					 <td colspan='2'>".nl2br($child[5])."</td>
				  </tr>
				  <tr colspan='3'>
					 <td>&nbsp;</td>
				  </tr>
				  <tr>
					 <td width='20%'>Special Notes:</td>
					 <td colspan='2'>".nl2br($child[6])."</td>
				  </tr>
				</table>
				<div align='center'>
				<p><strong>Adults Attached to Account</strong></p>
				  ".$adults."
				</div>";
			$temp->jqPopup("", "");
			echo "<script type='text/javascript'>loadPopupCalls()</script>";
		}
	}
}
